package cn.zhaoyuening.fanblog.admin.web;

import cn.zhaoyuening.fanblog.admin.service.ArticleTagService;
import cn.zhaoyuening.fanblog.admin.utils.ResultObj;
import cn.zhaoyuening.fanblog.common.domain.ArticleTag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "admin")
public class AdminArticleTagController {
	@Autowired
	private ArticleTagService articleTagService;
	
	@RequestMapping(path = "articleTags",method = RequestMethod.GET)
	public List<ArticleTag> findAll(){
		return articleTagService.findAll();
	}


	@RequestMapping(path = "articleTag/{name}", method = RequestMethod.PUT)
	public ResultObj updateTag(@RequestBody ArticleTag type, @PathVariable String name) {
		type.setName(name);
		boolean result = articleTagService.update(type);
		return ResultObj.getByResult(result);
	}

	@RequestMapping(path = "articleTag", method = RequestMethod.DELETE)
	public ResultObj deleteTagByList(@RequestBody List<ArticleTag> typeList) {
		articleTagService.deleteByList(typeList);
		return ResultObj.ok();
	}

	@RequestMapping(path = "articleTag", method = RequestMethod.POST)
	public ResultObj addArticleTag(String name) {
		ArticleTag articleTag = new ArticleTag().setName(name).setCount(0l);
		boolean result = articleTagService.add(articleTag);
		return ResultObj.getByResult(result);
	}

}
